<template>
  <div>
    <el-card id="search">
      <el-input v-model="searchModel.roleName" placeholder="按角色名查询" clearable></el-input>
      <el-button type="primary" round icon="el-icon-search" @click="getRoleList">查询</el-button>
      <el-button type="primary" icon="el-icon-plus" circle id="add" @click="EditAddUI"></el-button>
    </el-card>

    <el-card>
      <el-table
        :data="roleList"
        style="width: 100%">
        <el-table-column
          label="#"
          width="80">
          <template slot-scope="scope">
            {{ (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column
          prop="roleId"
          label="角色ID"
          width="200">
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色名"
          width="260">
        </el-table-column>
        <el-table-column
          prop="roleDesc"
          label="用户描述">
        </el-table-column>
        <el-table-column
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="EditUpdateUI(scope.row.roleId)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="deleteById(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearForm">
      <el-form :model="roleForm" :rules="rules" ref="roleFormRef">
        <el-form-item label="角色名称" prop="roleName" :label-width="formLabelWidth" style="width: 400px;">
          <el-input v-model="roleForm.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc" :label-width="formLabelWidth" style="width: 400px;">
          <el-input v-model="roleForm.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限设置" prop="menuIdList" :label-width="formLabelWidth" style="width: 400px;">
          <el-tree :data="menuList" :props="menuProps" node-key="menuId" ref="menuRef" @node-click="handleNodeClick" show-checkbox default-expand-all></el-tree>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import roleManager from '@/api/roleManager'
import menuManager from '@/api/menuManager'

export default {
  data() {
    return {
      menuList: [],
      menuProps: {
        children: 'children',
        label: 'title'
      },
      rules: {
        roleName: [
          { required: true, message: '请输入角色名', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      },
      formLabelWidth: '130px',
      dialogFormVisible: false,
      roleForm: {},
      title: '',
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 5,
      },
      roleList: []
    }
  },
  methods: {
    clearForm() {
      this.roleForm = {}
      this.$refs.roleFormRef.clearValidate()
      this.$refs.menuRef.setCheckedKeys([])
    },
    EditAddUI() {
      this.title = '新增角色'
      this.dialogFormVisible = true
    },
    EditUpdateUI(id) {
      this.title = '修改角色'
      this.dialogFormVisible = true
      roleManager.getRoleById(id).then(response => {
        this.roleForm = response.data
        this.$refs.menuRef.setCheckedKeys(response.data.menuIdList)
      })
    },
    deleteById(role) {
      this.$confirm(`此操作将永久删除角色 ${role.roleName}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        roleManager.deleteRole(role.roleId).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          });
          this.getRoleList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addRole() {
      this.$refs.roleFormRef.validate((valid) => {
        if (valid) {
          // alert(123)
          let checkedKeys = this.$refs.menuRef.getCheckedKeys();
          let halfCheckedKeys = this.$refs.menuRef.getHalfCheckedKeys();
          this.roleForm.menuIdList = checkedKeys.concat(halfCheckedKeys)
          roleManager.saveRole(this.roleForm).then(response => {
            this.$message({
              message: response.message,
              type: 'success'
            })
            this.dialogFormVisible = false
            this.getRoleList()
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    getRoleList() {
      roleManager.getRoleList(this.searchModel).then(response => {
        this.total = response.data.total
        this.roleList = response.data.rows
      })
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize
      this.getRoleList()
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo
      this.getRoleList()
    },
    getAllMenu() {
      menuManager.getAllMenu().then(response => {
        this.menuList = response.data
      })
    }
  },
  created() {
    this.getRoleList()
    this.getAllMenu()
  }
}
</script>

<style scoped>
#search .el-input{
  width: 200px;
  margin-right: 10px;
}

#add {
  float: right;
}
</style>
